package com.mc.read.adminapi.manager;

import com.alibaba.fastjson.JSON;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.dao.mapper.AdvertisingMapper;
import com.mc.read.dao.mapper.HotMapper;
import com.mc.read.dao.models.Advertising;
import com.mc.read.dao.models.Hot;
import com.mc.read.dao.models.sys.Sysuser;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author liuhao
 * @date 2019/6/26 15:34
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
public class AdvertisingManager {
    @Autowired
    AdvertisingMapper advertisingMapper;
    @Autowired
    HotMapper hotMapper;
    @Autowired
    CacheManager cacheManager;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询广告列表 selectAdverList
     **/
    public BaseRespose selectAdverList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        List<Advertising> advertisingList = advertisingMapper.selectByExample(null);
        baseRespose.setReturnString(advertisingList);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改广告信息 updateAdver
     **/
    public BaseRespose updateAdver(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Advertising advertising = null;
        try {
            advertising = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Advertising.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(advertising.getAdvertisingid())) {
            throw new ServiceException(203020, "请求信息不完整", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser=cacheManager.getSysUser(baseRequest.getUserId());
        advertising.setLocation(null);
        advertising.setUpdatedate(new Date());
        advertising.setUpdateuserid(sysuser.getUserid());
        advertising.setUpdateusername(sysuser.getName());
        advertisingMapper.updateByPrimaryKeySelective(advertising);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询热点品论 selectHot
     **/
    public BaseRespose selectHot(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Hot hot = hotMapper.selectByPrimaryKey(1);
        baseRespose.setReturnString(hot);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改热门品论 updateHot
     **/
    public BaseRespose updateHot(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Hot hot = null;
        try {
            hot = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Hot.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        Sysuser sysuser=cacheManager.getSysUser(baseRequest.getUserId());
        hot.setHotid(1);
        hot.setUpdatedate(new Date());
        hot.setUpdateuserid(sysuser.getUserid());
        hot.setUpdateusername(sysuser.getName());
        hotMapper.updateByPrimaryKeySelective(hot);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }


}
